$(function(){
	initList();
	$("#searchbtn").click();
});
var PAGE_SIZE=10;
var blank='<tr><td colspan="11">暂无数据...</td></tr>';
var pagingbar='<tr><td colspan="11">pagingbar</td></tr>';
var orderTemplate='<tr><td><a href="/order/order.id" target="_blank">order.id</a></td><td>order.num</td><td>order.price</td><td>order.username</td><td>order.tel</td><td>order.uid</td><td><a href="/order.pic1">正面</a> <a href="/order.pic2">反面</a></td><td>order.address</td><td>order.remark</td><td>order.createtime</td><td>passbtn</td></tr>';
var passbtn='<a href="#" checkorder="order.id" class="btn btn-small btn-danger">审核通过</a>';

function initList(){
	$("a[page]").live("click",function(){
		var page=$(this).attr("page")*1;
		searchOrders(page,PAGE_SIZE);
	});
	$("#searchbtn").click(function(){
		searchOrders(1,PAGE_SIZE);
	});
	//审核按钮
	$("a[checkorder]").live("click",function(){
		var id=$(this).attr("checkorder");
		if(!confirm("确定审核通过么？")){
			return false;
		}
		$.ajax({
			url:"/man/order/check/"+id,
			type:"post",
			success:function(res){
				if(res.success){
					alert("审核成功!");
					var page=$('li[class="disabled"] a:first').text()*1;
					searchOrders(page,PAGE_SIZE);
				}else{
					alert("审核失败!");
				}
			}
		});
			
	});
}
/**
 * 根据条件搜索订单
 * @param {} page
 * @param {} pageSize
 */
function searchOrders(page,pageSize){
	var data={};
	data.page=page<=0?1:page;
	data.pagesize=pageSize<=0?PAGE_SIZE:pageSize;
	$("#searchForm input").each(function(){
		var name=$(this).attr("name");
		var value=$(this).val();
		data[name]=value;
	});
	$.ajax({
		url:"/man/order/find",
		data:data,
		type:"post",
		success:function(res){
			$("#orderList").empty();
			var html="";
			if(res.total>0){
				for(var i=0;i<res.list.length;i++){
					var tmp=orderTemplate;
					var order=res.list[i];
					for(var p in order){
						var reg=new RegExp("order."+p,"g");
						if(p=='createtime'){
							order[p]=parseLong2DateString(order[p]);
						}
						if(p=="state"){
							if(order[p]==0){
								var passbtntmp=passbtn;
								passbtntmp=passbtntmp.replace("order.id",order.id);
								tmp=tmp.replace("passbtn",passbtntmp);
							}else{
								tmp=tmp.replace("passbtn","已审核");
							}
						}
						tmp=tmp.replace(reg,order[p]);
					}
					html+=tmp;
				}
				//添加分页烂
				var pagebar=pagingbar;
				var pagebarhtml=buildPagingBar(data.page,data.pagesize,res.total);
				pagebar=pagebar.replace("pagingbar",pagebarhtml);
				html+=pagebar;
			}else{
				html=blank;
			}
			$("#orderList").html(html);
		}
	});
}

